<template>
  <main class="header">
    <section class="header-logo">
      <i class="el-icon-date"></i>
      <p>工时管理</p>
    </section>
    <section class="header-user" @click="handleUser">
      <el-avatar
        size="small"
        src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
      >
      </el-avatar>
      <p class="user-name">魂魂张</p>
      <section class="pop">
        <el-popover
          placement="bottom"
          title="标题"
          width="200"
          v-model="popVisible"
          trigger="click"
          :visible-arrow="true"
        >
          <section class="pop-body">
            <el-button @click="handleLogout">退出登录</el-button>
          </section>
        </el-popover>
      </section>
    </section>
  </main>
</template>

<script>
export default {
  name: 'Header',
  data () {
    return {
      popVisible: false
    }
  },
  methods: {
    handleLogout () {
      console.log('退出登录', this.$store.state.login.token);
      const loading = this.$loading({
        lock: true,
        text: '正在退出登录...',
        spinner: 'el-icon-loading',
        background: 'rgba(255,255,255,0.5)'
      });
      this.$store.commit('setToken', '');
      localStorage.setItem('token', '');
      setTimeout(() => {
        if (this.$store.getters.getToken === '') {
          loading.close();
          this.$router.push({ path: '/login' });
          this.$message({
            type: 'success',
            message: '退出登录成功',
            center: true,
            customClass: 'log-out-msg'
          })
        }
      }, 1000);
    },
    handleUser () {
      console.log('点击用户列表显示');
      this.popVisible = !this.popVisible;
    }
  }
}
</script>

<style lang="scss" scoped>
.header {
  width: calc(100% - 0.1rem);
  height: 5rem;
  position: relative;
  border-bottom: 1px dashed #aaaaaa;
  .header-logo {
    width: 12rem;
    height: 100%;
    line-height: 100%;
    text-align: center;
    display: flex;
    align-content: center;
    align-items: center;
    position: absolute;
    left: 8rem;
    // border: 1px solid red;
    i {
      font-size: 1.8rem;
      color: #2696ff;
    }
    p {
      font-size: 1.6rem;
      color: #2696ff;
      margin-left: 0.5rem;
    }
  }
  .log-out-msg {
    width: 12rem;
  }
  .header-user {
    // width: 20rem;
    padding: 0 1rem;
    height: 100%;
    position: absolute;
    right: 10rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    cursor: pointer;
    .user-name {
      font-size: 1.6rem;
      margin-left: 1rem;
      color: #888888;
    }
    .user-name:hover {
      transition: all 0.5s ease-in-out;
      color: #2696ff;
    }
    .pop {
      position: absolute;
      bottom: -0.5rem;
      left: -5rem;
    }
  }
}
</style>